<template>
<div class="pb50">
    <el-form
      size="small"
      ref="form"
      :model="form"
      label-width="150px"
      v-if="!loading"
    >
      <!--商品信息-->
      <Info></Info>

      <!--规格-->
      <Spec></Spec>

      <!--其它-->
      <Other></Other>

      <!--提交-->
    </el-form>

    <div class="common-button-wrapper">
      <el-button size="small" type="info" @click="cancelFunc">{{$t('plus.cancel_text')}}</el-button>
      <el-button
        size="small"
        type="primary"
        @click="onSubmit"
        :loading="loading"
        >{{$t('plus.submit')}}</el-button
      >
    </div>
  </div>
</template>

<script>
import AdvanceApi from "@/api/advance.js";
import Info from "./part/Info.vue";
import Spec from "./part/Spec.vue";
import Other from "./part/Other.vue";
export default {
  components: {
    /*产品基本信息*/
    Info,
    /*规格*/
    Spec,
    /*其它*/
    Other
  },
  data() {
    return {
      /*是否正在加载*/
      loading: true,
      /*表单*/
      form: {
        /*商品ID*/
        advanceProductId: 0,
        /*商品状态*/
        status: 10,
        /*商品名称*/
        productName: "",
        /*图片*/
        productImage: "",
        /*排序*/
        sort: 100,
        /*规格*/
        productSkuId: 0,
        /*限购数量*/
        limitNum: 1,
        /*定金*/
        money: "",
        /*尾款立减金额*/
        reduceMoney: "",
        /*活动时间*/
        activeTime: [],
        /*商品表格*/
        tableData: []
      }
    };
  },
  provide: function () {
    return {
      form: this.form,
      type: "add"
    };
  },
  created() {
    if (this.$route.query.advanceProductId != null) {
      this.form.advanceProductId = this.$route.query.advanceProductId;
      this.getData();
    }
  },
  methods: {
    /*获取商品*/
    getData() {
      let self = this;
      AdvanceApi.detail({
        advanceProductId: self.form.advanceProductId
      }, true).then(res => {
        self.form.productName = res.data.productName;
        self.form.productImage = res.data.imagePath;
        self.form.productId = res.data.productId;
        if (res.data.startTime && res.data.endTime) {
          self.form.activeTime = [res.data.startTime, res.data.endTime];
        }
        // self.form.activeTime = res.data.activeTime;
        self.form.money = res.data.money;
        self.form.limitNum = res.data.limitNum;
        self.form.status = res.data.status;
        self.form.sort = res.data.sort;
        self.form.stock = res.data.stock;
        self.form.reduceMoney = res.data.reduceMoney;
        self.form.auditStatus = res.data.auditStatus;
        res.data.sku.forEach(item => {
          item.productName = self.form.productName;
          item.specName = item.productAttr;
          item.specType = res.data.specType;
        });
        self.form.tableData = res.data.sku;
        self.loading = false;
      });
    },
    /*提交表单*/
    onSubmit() {
      let self = this;
      self.$refs.form.validate(valid => {
        if (valid) {
          let params = {};
          params.productId = self.form.productId;
          params.money = self.form.money;
          params.status = self.form.status;
          params.sort = self.form.sort;
          params.limitNum = self.form.limitNum;
          if (self.form.activeTime && self.form.activeTime.length > 0) {
            params.startTime = self.form.activeTime[0];
            params.endTime = self.form.activeTime[1];
          }
          params.reduceMoney = self.form.reduceMoney;
          params.advanceProductId = self.form.advanceProductId;
          params.productId = self.form.productId;
          params.auditStatus = self.form.auditStatus;
          params.specList = self.tableFormet(self.form.tableData);
          self.loading = true;
          AdvanceApi.saveData(params, true).then(data => {
            self.loading = false;
            ElMessage({
              message: $t("plus.add_success"),
              type: "success"
            });
            self.cancelFunc();
          }).catch(error => {
            self.loading = false;
          });
        }
      });
    },
    /*表格数据格式化*/
    tableFormet(list) {
      list.forEach(item => {
        item.productAttr = item.specName;
      });
      return list;
    },
    /*取消*/
    cancelFunc() {
      this.$router.back(-1);
    }
  }
};
</script>

<style scoped>
.edit_container {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
}

.ql-editor {
  height: 400px;
}
</style>

